package com.xbongbong.saas.service.toolbox.handle;

import com.alibaba.fastjson.JSONObject;
import com.xbongbong.paas.domain.entity.ext.ConditionsEntityExt;
import com.xbongbong.paas.enums.ConditionEnum;
import com.xbongbong.paas.toolbox.util.StringUtil;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Objects;

/**
 * @author: wujian
 * @create: 2021-01-15 17:22
 * @description: 数组和map类型数据数据匹配规则抽象类
 **/
@Component
public class WebCommonDataValueMatchHandle {
    public boolean match(ConditionsEntityExt conditionsEntityExt, JSONObject data){
        ConditionEnum conditionEnum = ConditionEnum.getConditionEnum(conditionsEntityExt.getSymbol());
        if (Objects.isNull(conditionEnum)) {
            return false;
        }
        String attr = conditionsEntityExt.getAttr();
        List<Object> value = conditionsEntityExt.getValue();
        Object dataValue = data.get(attr);
        switch (conditionEnum) {
            case EQUAL:
            case ALLINCLUDE:
                if (!emptyCheck(value, dataValue, 3) || !Objects.equals(Objects.toString(value.get(0), ""), dataValue.toString())) {
                    return false;
                }
                break;
            case NOEQUAL:
                if (!emptyCheck(value, dataValue, 3) || Objects.equals(Objects.toString(value.get(0), ""), dataValue.toString())) {
                    return false;
                }
                break;
            case LIKE:
            case INCLUDE:
            case IN:
                boolean not = true;
                if (Objects.isNull(dataValue) || StringUtil.isEmpty(dataValue.toString()) || Objects.isNull(value) || value.isEmpty()) {
                    return false;
                }
                for (Object obj : value) {
                    if (dataValue.toString().contains(obj.toString())) {
                        not = false;
                        break;
                    }
                }
                if (!emptyCheck(value, dataValue, 3) || not) {
                    return false;
                }
                break;
            case NOLIKE:
            case NOIN:
                if (!emptyCheck(value, dataValue, 3) || dataValue.toString().contains(value.get(0).toString())) {
                    return false;
                }
                break;
            case EMPTY:
                return !emptyCheck(value, dataValue, 1);
            case NOEMPTY:
                return emptyCheck(value, dataValue, 1);
            case GREATERTHAN:
                if (!emptyCheck(value, dataValue, 3) || data.getDouble(attr) <= Double.parseDouble(value.get(0).toString())) {
                    return false;
                }
                break;
            case GREATEREQUAL:
                if (!emptyCheck(value, dataValue, 3) || data.getDouble(attr) < Double.parseDouble(value.get(0).toString())) {
                    return false;
                }
                break;
            case LESSEQUAL:
                if (!emptyCheck(value, dataValue, 3) || data.getDouble(attr) > Double.parseDouble(value.get(0).toString())) {
                    return false;
                }
                break;
            case LESSTHAN:
                if (!emptyCheck(value, dataValue, 3) || data.getDouble(attr) >= Double.parseDouble(value.get(0).toString())) {
                    return false;
                }
                break;
            case RANGE:
                if (!emptyCheck(value, dataValue, 3) || value.size() < 2 || data.getDouble(attr) < Double.parseDouble(value.get(0).toString()) || data.getDouble(attr) > Double.parseDouble(value.get(1).toString())) {
                    return false;
                }
                break;
            case NORANGE:
                if (!emptyCheck(value, dataValue, 3) || value.size() < 2 || (data.getDouble(attr) > Double.parseDouble(value.get(0).toString()) && data.getDouble(attr) < Double.parseDouble(value.get(1).toString()))) {
                    return false;
                }
                break;
            default:
                break;
        }
        return true;
    }

    private boolean emptyCheck(List<Object> value, Object dataValue, Integer checkType) {
        if (checkType == 1) {
            if (Objects.isNull(dataValue) || StringUtil.isEmpty(dataValue.toString())) {
                return false;
            }
        } else if (checkType == 2) {
            if (Objects.isNull(value) || value.isEmpty()) {
                return false;
            }
        } else {
            if (Objects.isNull(dataValue) || StringUtil.isEmpty(dataValue.toString()) || Objects.isNull(value) || value.isEmpty()) {
                return false;
            }
        }
        return true;
    }
}
